package com.xczl.book.test.service;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.xczl.book.entity.BookCate;
import com.xczl.book.service.BookCateService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:applicationContext.xml" })
public class BookCateServiceTestCase {

	@Autowired
	private BookCateService bookCateService;

	/**
	 * 添加图书分类
	 */
	@Test
	@Transactional
	public void addBookCate() {
		BookCate bookCate = createBookCate();
		bookCateService.add(bookCate);
		BookCate temp = bookCateService.findById(bookCate.getCateId());
		Assert.notNull(temp);
	}

	/**
	 * 创建图书分类
	 * 
	 * @return
	 */
	private BookCate createBookCate() {
		BookCate bookCate = new BookCate();
		bookCate.setName("幼儿");
		return bookCate;
	}

	/**
	 * 修改图书分类
	 */
	@Test
	@Transactional
	public void update() {
		// 注册
		BookCate bookCate = createBookCate();
		bookCateService.add(bookCate);
		// 修改商品
		bookCate.setName("漫画");
		bookCateService.update(bookCate);
		// 获取更新信息
		BookCate bookCate1 = bookCateService.findById(bookCate.getCateId());
		// 验证结果
		Assert.isTrue(StringUtils.equals(bookCate.getName(),
				bookCate1.getName()));
	}
}
